#include "Protocols/PPP_PProtocal.h"
#include "utilities/ReadablePacket.h"
#include "protocols/ProtocalCode.h"
#include "protocols/ProtocalPool.h"
#include "protocols/ShowableItem.h"
#include "utilities/utilities.h"

REGISTER_PROTOCOL(PPP_PProtocal)

void PPP_PHeader::toShowableItems(ShowableItem* root) const
{
	HeaderFormat* header = (HeaderFormat*) mData;
	int pos = mPos;
	ShowableItem *temp;

	ShowableItem* proto = createProtocalItem("Point-to-Point Protocol", pos, getHeaderLength());
	root->pushChild(proto);

	temp = createItem("Protocol:", getProtolInfo(), pos, 2);
	proto->pushChild(temp);
}

QString PPP_PHeader::getProtolName() const
{
	switch(getTypeCode())
	{
	case 33:
		return QString("IP");
	default:
		return QString("unknown");
	}
}

PPP_PProtocal::PPP_PProtocal() : Protocol(PPP_PPPOE, QString("PPP"))
{
}


PacketHeader*
PPP_PProtocal::analyzeProc(ReadablePacket*packet,int low,int high,Protocol*& tip)
{
	PPP_PHeader* header = new PPP_PHeader(PPP_PProtocal_register.m_Instance,packet->getData(),low,high - low + 1);
	static Protocol* ipPtr = ProtocolPool::getProtocal(IPV4).get();

//	tip = m_PtrProtocolTable[header->getTypeCode() % 34];

 	if (33 == header->getTypeCode())
		tip = ipPtr;
	else
		tip = NULL;

	return header;
}

QString PPP_PHeader::getProtolInfo() const
{
	return QString(" %1 (%2)").arg(getProtolName()).arg(toHexString((const unsigned char *) &((HeaderFormat*)mData)->_mProtol, 2));
}